package com.wxprogram.smallprogram.service.impl;

import com.wxprogram.smallprogram.dao.IFindstudent;
import com.wxprogram.smallprogram.dao.IFindteacher;
import com.wxprogram.smallprogram.entity.FindStudent;
import com.wxprogram.smallprogram.entity.FindTeacher;
import com.wxprogram.smallprogram.service.FileService;
import org.apache.commons.lang.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class FileServiceImpl implements FileService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IFindstudent iFindstudent;
    @Autowired
    private IFindteacher iFindteacher;

    @Override
    public int insertImg(int degree, int code, String filepath) {

        if (degree == 1){
            FindTeacher findTeacher = new FindTeacher();
            findTeacher.setStucode(code);
            findTeacher.setImg(filepath);
            return iFindteacher.updateFindteacher(findTeacher);
        }else {
            FindStudent findStudent = new FindStudent();
            findStudent.setTeachercode(code);
            findStudent.setImg(filepath);
            return iFindstudent.updateFindstudent(findStudent);
        }
    }

    /**
     * 删除服务器图片
     * @param path
     */
    @Override
    public void deleteFile(String path) {
        /**
         * Linux 下运行
         */
        if (SystemUtils.IS_OS_LINUX){
            File file = new File(path);
            if (file.exists()){
                file.delete();
            }
        }
        /**
         * windows下测试
         */
        if (SystemUtils.IS_OS_WINDOWS){
            File file = new File(path);
            if (file.exists()){
                file.delete();
            }
            System.out.println("这是Windows系统");
        }

    }
}
